<?php
//$_GET['qid']=question id
//
//$_POST['qid']=question id
//$_POST['level']
//$_POST['question']
//$_POST['answer']

	include 'include/function.php';

	if(!isset($_user_id))redirect_error(_d('err_need_login'));
	//获取question id,node id,..
	if(isset($_GET['qid'])) {
		$editquestion_qid=get_to_mysql($_GET['qid']);
		$query="select * from question where question_id=$editquestion_qid ";
		$ret=oj_query($query);
		$row=mysql_fetch_assoc($ret);
		if(isset($row['node_id'])) {
			$editquestion_id		=$row['node_id'];
			$editquestion_level		=$row['level'];
			$editquestion_question	=$row['question'];
			$editquestion_answer	=$row['answer'];
		}
	} else if(isset($_POST['qid'])) {
		$editquestion_qid=get_to_mysql($_POST['qid']);
		$query="select node_id from question where question_id=$editquestion_qid ";
		$ret=oj_query($query);
		$row=mysql_fetch_row($ret);
		if(isset($row[0])) {
			$editquestion_id		=$row[0];
			$editquestion_level		=get_to_mysql($_POST['level']);
			$editquestion_question	=get_to_mysql($_POST['question']);
			$editquestion_answer	=get_to_mysql($_POST['answer']);
		}
	} else redirect('./');
	
	if(!isset($editquestion_id))redirect_error(_d('err_no_question'));
	//权限限制
	$editquestion_perm=permission_check_edit($_user_id,$editquestion_id);
	if(!strstr($editquestion_perm[0],$editquestion_level))
		redirect_error("You can't edit the question of $editquestion_level, because you havn't it.");
	
	if(isset($_POST['level'])){
		//更新
		$query="update question set level='$editquestion_level', question='$editquestion_question', "
			. "answer='$editquestion_answer', edit_time=now() where question_id=$editquestion_qid ";
		$ret=oj_query($query);
		
		redirect("showquestion.php?id=$editquestion_id");
	}
	
function draw_fillin($left,$input,$name,$prop='',$right='',$value=''){
	echo '<tr>';
	echo '<td>' . str_replace("\n",'<br>',htmlspecialchars($left)) . '</td>';
	echo "<td><$input name='$name' $prop>" . htmlspecialchars($value) . "</$input>$right</td>";
	echo '</tr>';
}

	include 'include/header.php';
	echo "<title>$site_title - Edit Question</title>";
?>

<form method=post action="editquestion.php">
<?php echo "<input type='hidden' name='qid' value='$editquestion_qid'>"; ?>
<table align=center>
	<tr>
		<td>Question ID:</td>
		<td><?php echo $editquestion_qid; ?></td>
	</tr>
	<tr>
		<td>Node ID:</td>
		<td><?php echo "<a href='shownode.php?id=$editquestion_id'>$editquestion_id</a>"; ?></td>
	</tr>
	<tr>
		<td>Level:</td>
		<td>
			<?php
				echo '<select name="level">';
				foreach($level_array as $key=>$val){
					if(strstr($editquestion_perm[0],$key)){
						echo "<option value='$key'";
						if('view'==$key&&strlen('view')==strlen($key))echo ' selected';
						echo ">$val</option>";
					}
				}
				echo '</select>';
			?>
		</td>
	</tr>
<?php
	draw_fillin('Question(html):','textarea','question','','',$editquestion_question);
	draw_fillin("Answer\n(please enter the acceptable answers \nusing this format: 'a','b','c'..., \nthe answers are case insensitive):",'textarea','answer','','',$editquestion_answer);
?>
	<tr>
		<td></td>
		<td><input type='submit' name='submit' value='Submit'/></td>
	</tr>
</table>
</form>

<?php
	include 'include/footer.php';
?>

